import { flatOldGate,addGate,openGate,closeGate } from './addFloodgate'
import  Manyou  from './manyou'
import { addArea  } from './riverWater'


export default class Effect3 {
  constructor(viewer){
    this.viewer = viewer
    this.tile3d = null
    this.tile3d1 = null

    this.manyou = null

    this.start()
  }

  start(){

    this.startManyou()
      // this.viewer.postProcessStages.hdrBloom.enabled = true
      var tile = new Cesium.Cesium3DTileset({
        url: 'http://59.231.31.20/platform/3d/Data/3dtiles/tileset.json',
        maximumMemoryUsage : 5000,
        useIndexDB:true
      });
  
      this.tile3d =  this.viewer.scene.primitives.add(tile);

      this.tile3d.readyPromise.then((tileset)=>{
        flatOldGate(this.viewer,tileset);
        addGate(this.viewer);

        //水闸默认常开
        openGate(this.viewer,[0,1,2,3]);

        const heightOffset = 16.0;
        const boundingSphere = tileset.boundingSphere;
        const cartographic = Cesium.Cartographic.fromCartesian(boundingSphere.center);
        const surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, 0.0);
        const offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, heightOffset);
        const translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3());
        tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
      });
      // addLayer(this.viewer , window.serviceUrl.yanmoLayer)
  }

  startManyou(){
    
    //漫游的时候水位上涨了
    addArea()

    this.viewer.camera.flyTo({
      destination: Cesium.Cartesian3.fromDegrees(113.15385092494088,27.872349027677906, 1505.8264722325495), //设置位置
      orientation: {
        heading: Cesium.Math.toRadians(241.3212617166511),
        pitch: Cesium.Math.toRadians(-33.204682831573585),
        roll: 0
      },

    });
    this.manyou = new Manyou(this.viewer , [
      [113.14516077436889,27.867865753636963,50],
      [113.14347579354299,27.866867824812285,50],
      [113.14139387488969,27.865681087739468,50],
      [113.13830520454766,27.864121047873464,50],
      [113.13528406182836,27.86272338639541,50]
    ])
  }
  

  clearTimer(){
    this.manyou&&this.manyou.clear()
  }

  clear(){
   
    this.clearTimer()
    
    this.tile3d&&this.viewer.scene.primitives.remove(this.tile3d);

    this.tile3d1&&this.viewer.scene.primitives.remove(this.tile3d1);

  }

}